package com.example.wangzh.developkit.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.InputFilter;
import android.util.AttributeSet;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.utils.UIUtils;

import java.util.Arrays;

/**
 * Created by zhwang16 on 2018/3/19.
 */

public class UnderlineEditText extends androidx.appcompat.widget.AppCompatEditText {
    private static final String TAG = UnderlineEditText.class.getName();
    private static float DEFAULT_LINE_WIDTH = 3;//默认线框宽度
    private static final int DEFAULT_LINE_COLOR = Color.BLACK;//默认线框颜色
    private static final float DEFAULT_TEXT_SIZE = 20;
    private static final String DEFAULT_PASS_SHOW = "*";

    private float lineWidth = DEFAULT_LINE_WIDTH;
    private int lineColor = DEFAULT_LINE_COLOR;
    private int textColor = DEFAULT_LINE_COLOR;
    private float textSize = DEFAULT_TEXT_SIZE;
    private final String pass = DEFAULT_PASS_SHOW;
    private int mCount = 11;
    private Paint mPaint;
    private int divideWidth = 40;
    //    1 2 3   4 5
    //    a b c   d e
    //            ↑
    //          dividePos
    private int[] dividePos = new int[]{4, 8};
    private float underLineWidth = 80;

    public UnderlineEditText(Context context) {
        super(context);
    }

    public UnderlineEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化
//        this.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
        this.setBackgroundColor(Color.TRANSPARENT);
        this.setCursorVisible(false);
        this.setPadding(0, 0, 0, 0);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.UnderlineEditText);
        lineWidth = typedArray.getDimension(R.styleable.UnderlineEditText_lineWidth, DEFAULT_LINE_WIDTH);
        lineColor = typedArray.getColor(R.styleable.UnderlineEditText_lineColor, DEFAULT_LINE_COLOR);
        textColor = typedArray.getColor(R.styleable.UnderlineEditText_textColor, DEFAULT_LINE_COLOR);
        textSize = typedArray.getDimension(R.styleable.UnderlineEditText_textSize, UIUtils.dip2Px((int) DEFAULT_TEXT_SIZE));
        typedArray.recycle();

        mPaint.setStrokeWidth(lineWidth);
        mPaint.setTextSize(textSize);

        this.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mCount)});
        int curPosition = 0;
        for (int i = 0; i < dividePos.length; i++) {
            curPosition += dividePos[i];
            if (curPosition >= mCount) {
                dividePos = Arrays.copyOf(dividePos, i - 1);
            }
            break;
        }
    }

    public UnderlineEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);

        int w = getMeasuredWidth();
        int h = getMeasuredHeight();

        int cellW = (w - divideWidth * dividePos.length) / mCount;

        //绘制背景线框
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(lineColor);
        float curLineLeft = 0;
        float linePadding = (cellW - underLineWidth) / 2;
        if (linePadding < 0) {
            linePadding = (float) (cellW * 0.2);
            underLineWidth = cellW - 2 * linePadding;
        }
        for (int i = 0; i < mCount; i++) {
            curLineLeft += linePadding;
            if (hasLeftDivider(i)) {
                curLineLeft += divideWidth;
            }
//            canvas.translate(curLineLeft, h);
            canvas.drawLine(curLineLeft, h, curLineLeft + underLineWidth, h, mPaint);
            curLineLeft += underLineWidth;
            curLineLeft += linePadding;
        }

        //绘制内容
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(textColor);
        canvas.save();
        if (getText().length() > 0) {
            canvas.translate((cellW - mPaint.measureText(getText().charAt(0) + "")) / 2, 0);
        }
        Paint.FontMetrics metrics = mPaint.getFontMetrics();
        float y = (int) (h / 2 + Math.abs((metrics.ascent + metrics.descent) / 2));
        for (int i = 0; i < getText().length(); i++) {
            canvas.drawText(getText().charAt(i) + "", 0, y, mPaint);
            if (hasLeftDivider(i + 1)) {
                canvas.translate(cellW + divideWidth, 0);
            } else {
                canvas.translate(cellW, 0);
            }
        }
        canvas.restore();
    }

    private boolean hasLeftDivider(int i) {
        for (int j = 0; j < dividePos.length; j++) {
            if (i == dividePos[j] - 1) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    private int measureWidth(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = 0;
        switch (mode) {
            case MeasureSpec.EXACTLY:
                size = MeasureSpec.getSize(widthMeasureSpec);
                break;
            case MeasureSpec.AT_MOST:
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < mCount; i++) {
                    sb.append(" " + pass + " ");
                }
                size = (int) mPaint.measureText(sb.toString());
                size += (dividePos.length) * divideWidth;
                break;
        }
        return size;
    }

    private int measureHeight(int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = 0;
        switch (mode) {
            case MeasureSpec.EXACTLY:
                size = MeasureSpec.getSize(heightMeasureSpec);
                break;
            case MeasureSpec.AT_MOST:
                Paint.FontMetrics metrics = mPaint.getFontMetrics();
                size = (int) (metrics.bottom - metrics.top + 2 * lineWidth) + 1;
                break;
        }
        return size;
    }


}
